package com.zhenwei.zscm.controller;

import cn.hutool.core.util.StrUtil;
import com.zhenwei.zscm.common.annotations.Insert;
import com.zhenwei.zscm.common.annotations.MethodName;
import com.zhenwei.zscm.common.annotations.Update;
import com.zhenwei.zscm.common.base.RestResult;
import com.zhenwei.zscm.model.vo.order.CreateOrderVO;
import com.zhenwei.zscm.model.vo.order.ExportVO;
import com.zhenwei.zscm.model.vo.order.QueryOrderVO;
import com.zhenwei.zscm.service.OrderService;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/order")
@AllArgsConstructor
@MethodName("售后工单管理")
public class OrderController {
    private OrderService service;

    @MethodName("获取工单编号")
    @GetMapping("/getOrderNumber")
    public RestResult getOrderNumber(){
        return service.getOrderNumber();
    }

    @MethodName("创建工单")
    @PostMapping("/create")
    public RestResult create(@RequestBody @Validated(Insert.class) CreateOrderVO vo) {
        return service.create(vo);
    }

    @MethodName("修改工单")
    @PostMapping("/update")
    public RestResult update(@RequestBody @Validated(Update.class) CreateOrderVO vo) {
        return service.update(vo);
    }

    @MethodName("删除工单")
    @GetMapping("/delete")
    public RestResult delete(@RequestParam @NotEmpty(message = "订单ID不能为空") String orderId) {
        return service.delete(orderId);
    }
    @MethodName("作废工单")
    @GetMapping("/invalid")
    public RestResult invalid(@RequestParam @NotEmpty(message = "订单ID不能为空") String orderId) {
        return service.invalid(orderId);
    }
    @MethodName("获取工单详情")
    @GetMapping("/detail")
    public RestResult detail(@RequestParam  @NotEmpty(message = "订单ID不能为空") String orderId) {
        return service.detail(orderId);
    }
    @MethodName("获取工单基本信息")
    @GetMapping("/baseInfo")
    public RestResult getBaseInfo(@RequestParam  @NotEmpty(message = "订单ID不能为空") String orderId) {
        return service.getBaseInfo(orderId);
    }
    @MethodName("获取工单列表")
    @PostMapping("/page")
    public RestResult page(@RequestBody QueryOrderVO query) {
        return service.page(query);
    }

    @MethodName("获取产品明细")
    @GetMapping("/product/detail")
    public RestResult getProductDetail(@RequestParam @NotEmpty(message = "订单ID不能为空") String orderId, @RequestParam @NotEmpty(message = "产品SN不能为空") String productSn) {
        return service.getProductDetail(orderId, productSn);
    }

    @MethodName("获取产品维修次数")
    @GetMapping("/product/repairCount")
    public RestResult getProductRepairCount(@RequestParam @NotEmpty(message = "SN不能为空") String productSn) {
        return service.getProductSnRepairCount(productSn);
    }
    @MethodName("导入发货数据")
    @GetMapping("/import")
    public RestResult importData(@RequestParam @NotEmpty(message = "文件ID不能为空") String fileId) {
        return service.importData(fileId);
    }

    @MethodName("导出发货数据")
    @PostMapping("/export")
    public RestResult export(@RequestBody ExportVO vo) {
        return service.export(vo);
    }

    @MethodName("生成检测报告单")
    @GetMapping("/generateReport")
    public RestResult generateReport(@RequestParam @NotEmpty(message = "工单ID不能为空") String orderId) {
        return service.generateReport(orderId);
    }

    @GetMapping("/customerSearch")
    public RestResult customerSearch(@RequestParam String searchKey, @RequestParam String captcha, HttpSession session) {
        String captchaText = (String) session.getAttribute("CAPTCHA_TEXT");
        if(StrUtil.isBlank(captcha) || !captcha.equals(captchaText)){
            return RestResult.error("验证码错误");
        }
        return service.customerSearch(searchKey);
    }

    @MethodName("删除指定产品")
    @GetMapping("/deleteProduct")
    public RestResult deleteProduct(@RequestParam @NotEmpty(message = "工单ID不能为空") String orderId, @RequestParam @NotEmpty(message = "产品SN不能为空") String productSn) {
        return service.deleteProduct(orderId, productSn);
    }
}
